package controller;

import org.lwjgl.input.Keyboard;
import vehement.game.Globals;


/**
 * Controller intakes inputs, and calls methods in world or gamethread
 * @author bloodred
 *
 */
public class Controller {

	public Controller(){}

	public static void createKeyboard() {
		try {
			Keyboard.create();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(-1);
		}
	}

	public static void wiggleKeyboard(float delta) {

		//    while (!Display.isCloseRequested()) {
		//      Display.update();
		//
		//      if (!Display.isVisible()) {
		//        try {
		//          Thread.sleep(100);
		//        } catch (InterruptedException inte) {
		//          inte.printStackTrace();
		//        }
		//        continue;
		//      }

		//check keys, buffered
		Keyboard.poll();

		int count = Keyboard.getNumKeyboardEvents();
		while (Keyboard.next()) {
			int character_code = ((int)Keyboard.getEventCharacter()) & 0xffff;
			System.out.println("Checking key:" + Keyboard.getKeyName(Keyboard.getEventKey()));
			System.out.println("Pressed:" + Keyboard.getEventKeyState());
			System.out.println("Key character code: 0x" + Integer.toHexString(character_code));
			System.out.println("Key character: " + Keyboard.getEventCharacter());
			System.out.println("Repeat event: " + Keyboard.isRepeatEvent());

			if (Keyboard.getEventKey() == Keyboard.KEY_R && Keyboard.getEventKeyState()) {
				Keyboard.enableRepeatEvents(!Keyboard.areRepeatEventsEnabled());
			}
			if (Keyboard.getEventKey() == Keyboard.KEY_ESCAPE) {
				return;
			}
		}
		//Camera controls
		boolean camchange = false;
		if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) {
			Globals.camAngle += delta * Globals.camAngleSpd;
			if (Globals.camAngle > 360.f){
				Globals.camAngle -= 360.f;
			}
			camchange= true;
		}

		if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
			Globals.camAngle -= delta * Globals.camAngleSpd;
			if (Globals.camAngle < 0.f){
				Globals.camAngle += 360.f;
			}
			camchange=true;
		}

		if (Keyboard.isKeyDown(Keyboard.KEY_UP)) {
			Globals.camHeight += delta * Globals.camHeightSpd;
			if (Globals.camHeight > Globals.camHeightMax){
				Globals.camHeight = Globals.camHeightMax;
			}
			camchange=true;
		}

		if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) {
			Globals.camHeight -= delta * Globals.camHeightSpd;
			if (Globals.camHeight < Globals.camHeightMin){
				Globals.camHeight = Globals.camHeightMin;
			}
			camchange=true;
		}
		//TODO: recalc camera
		if (camchange){
			Globals.camPos.x = (float) Math.cos(Globals.camAngle) * Globals.camDist + Globals.camTarg.x;
			Globals.camPos.y = (float) Math.sin(Globals.camAngle) * Globals.camDist + Globals.camTarg.y;
			Globals.camPos.z = Globals.camHeight;
		}
	//	Globals.camPos.x += 1;
	//	Globals.camPos.x -= 1;		
	//	Globals.camPos.y += 1;
	//	Globals.camPos.y -= 1;
		
		//character movement(wasd for now)
		
		
		if (count > 0) {
			System.out.println();
		}

//		if (Globals.camPos.x < 0) {
//			Globals.camPos.x = 0;
//		} else if (Globals.camPos.x > 640 - 60) {
//			Globals.camPos.x = 640 - 60;
//		}

//		if (Globals.camPos.y < 0) {
//			Globals.camPos.y = 0;
//		} else if (Globals.camPos.y > 480 - 30) {
//			Globals.camPos.y = 480 - 30;
//		}

	
	}
}


